//------------------------------------------------------------------------------
// <copyright company="Telligent Systems">
//     Copyright (c) Telligent Systems Corporation.  All rights reserved.
// </copyright> 
//------------------------------------------------------------------------------

using System;

using CommunityServer.Components;
using CommunityServer.Configuration;

namespace CommunityServer.Blogs.Components
{
    /// <summary>
    /// Summary description for BlogUrls.
    /// </summary>
    public class BlogUrls : ApplicationUrls
    {

		public static readonly string BlogUrlsProviderName = "BlogUrlsProvider";

		#region Constructor & Instance

		public BlogUrls() { }

		private static BlogUrls _defaultInstance = null;

		static BlogUrls()
		{
			CreateDefaultCommonProvider();
		}


		/// <summary>
		/// Returns an instance of the user-specified data provider class.
		/// </summary>
		/// <returns>An instance of the user-specified data provider class.  This class must inherit the
		/// CommonDataProvider interface.</returns>
		public static BlogUrls Instance() 
		{
			return _defaultInstance;
		}

		
		public static BlogUrls Instance (Provider dataProvider) 
		{
			BlogUrls fdp = CSCache.Get(dataProvider.Name) as BlogUrls;
			if(fdp == null)
			{
				fdp = DataProviders.Invoke(dataProvider) as BlogUrls;
				CSCache.Max(dataProvider.Name,fdp);
			}
			return fdp;
		}


		/// <summary>
		/// Creates the Default CommonDataProvider
		/// </summary>
		private static void CreateDefaultCommonProvider()
		{
			// Read the configuration specific information
			// for this provider
			//
			Provider urlProvider = (Provider) CSConfiguration.GetConfig().Providers[BlogUrlsProviderName];

			// Create the instance for the provider, or fall back on the builtin one
			//
			if(urlProvider != null)
				_defaultInstance = CreateInstance(urlProvider) as BlogUrls;
			else
				_defaultInstance = new BlogUrls();
		}
        
		
		#endregion

		#region Blog URLs

        #region Public Urls

        public virtual string Home
        {
            get
            {
                return FormatUrl("webloghome");
            }
        }

        public virtual string GroupHome(int GroupID)
        {
            return FormatUrl("weblog_GroupHome",GroupID);
        }

		public virtual string GroupRss()
		{
			return FormatUrl("blog_aggregate_Syndication_default");
		}

		public virtual string GroupRss(int GroupID, BlogMirrorDisplayType type)
		{
			if (GroupID < 1 && type == BlogMirrorDisplayType.AllBlogs)
				return GroupRss();
			else
				return FormatUrl("blog_aggregate_Syndication", GroupID, System.Enum.GetName(typeof(BlogMirrorDisplayType), type));
		}

		public virtual string GroupRss(int GroupID, string[] tagNames, bool andTags)
		{
			System.Text.StringBuilder sb = new System.Text.StringBuilder();
			foreach (string tag in tagNames)
			{
				if (sb.Length > 0)
					sb.Append("/");

				sb.Append(Globals.UrlEncodePathComponent(tag));
			}
			return FormatUrl("blog_aggregate_Syndication_tags", GroupID, sb.ToString(), Convert.ToInt32(andTags) );
		}

		public virtual string GroupOpml()
		{
			return FormatUrl("blog_aggregate_Opml_default");
		}

		public virtual string GroupOpml(int GroupID)
		{
			if (GroupID < 1)
				return GroupOpml();
			else
				return FormatUrl("blog_aggregate_Opml", GroupID);
		}

		public virtual string GroupBloggers()
		{
			return FormatUrl("blog_aggregate_Bloggers_default");
		}

		public virtual string GroupBloggers(int GroupID)
		{
			if (GroupID < 1)
				return GroupBloggers();
			else
				return FormatUrl("blog_aggregate_Bloggers", GroupID);
		}

        public virtual string GroupList(int GroupID)
        {
            return FormatUrl("weblog_GroupList",GroupID);
        }

        public virtual string ShortLink(int postid)
        {
            return FormatUrl("blog_ShortLink",postid);
        }

        public virtual string HomePage(string applicationKey)
        {
            
            return FormatUrl("weblogapplication",applicationKey);
        }

        public virtual string Rss(string applicationKey)
        {
            return FormatUrl("weblogIndividualRss",applicationKey);
        }

        public virtual string Rss(string applicationKey, int categoryID)
        {
            return FormatUrl("weblogCategoryRss",applicationKey,categoryID);
        }

		public virtual string Rss(string applicationKey, string[] tagNames)
		{
			System.Text.StringBuilder sb = new System.Text.StringBuilder();
			foreach (string tag in tagNames)
			{
				if (sb.Length > 0)
					sb.Append("/");

				sb.Append(Globals.UrlEncodePathComponent(tag));
			}
			return FormatUrl("weblogTagsRss", applicationKey, sb.ToString() );
		}
		
		public virtual string Rss(string applicationKey, string[] tagNames, bool andTags)
		{
			System.Text.StringBuilder sb = new System.Text.StringBuilder();
			foreach (string tag in tagNames)
			{
				if (sb.Length > 0)
					sb.Append("/");

				sb.Append(Globals.UrlEncodePathComponent(tag));
			}
			return FormatUrl("weblogTagsRssAnd", applicationKey, sb.ToString(), Convert.ToInt32(andTags) );
		}

		public virtual string RssPrivate(string applicationKey)
		{
			return FormatUrl("weblogIndividualPrivateRss", applicationKey);
		}

		public virtual string RssPost(string applicationKey, int postID)
		{
			return FormatUrl("weblogPostRss",applicationKey,postID);
		}

		public virtual string Atom(string applicationKey)
		{
			return FormatUrl("weblogIndividualAtom",applicationKey);
		}

		public virtual string AtomPrivate(string applicationKey)
		{
			return FormatUrl("weblogIndividualPrivateAtom", applicationKey);
		}

		public virtual string RssComments(string applicationKey, int PostID)
		{
			return FormatUrl("weblogRssComments", applicationKey, PostID);
		}
		
        public virtual string CommentsPage(string applicationKey, int PostID)
        {
            return FormatUrl("WeblogCommentsPage",applicationKey,PostID);
        }

		public virtual string CommentRss(string applicationKey, int PostID)
		{
			return FormatUrl("weblogCommentRss", applicationKey,PostID);
		}

        public virtual string MonthPage(string applicationKey,DateTime dt)
        {
            return FormatUrl("weblogmonth",applicationKey, dt.Year,dt.Month.ToString("00"));
        }

        public virtual string DayPage(string applicationKey, DateTime dt)
        {
            return FormatUrl("weblogday",applicationKey,dt.Year,dt.Month,dt.Day);
        }

        public virtual string Post(WeblogPost post)
        {
            return Post(post,post.Weblog);
        }

        public virtual string Post(WeblogPost post, Weblog weblog)
        {            
            // support writing out external links 
            // (instead of link to internal version of post)
			if (weblog.SyndicateExternalLinks && post.IsExternal)
			{
				return post.TitleUrl;
			}
				// create link for article
			else if(post.BlogPostType == BlogPostType.Article)
			{
				if(post.HasName)
					return FormatUrl("weblogarticleName",weblog.ApplicationKey,Globals.UrlEncodeFileComponent(post.Name));
				else
					return FormatUrl("weblogarticleId",weblog.ApplicationKey,post.PostID);
			}
			else if (post.BlogPostType == BlogPostType.Comment)
			{
				return FormatUrl("weblogpostId", weblog.ApplicationKey, post.UserTime.Year, post.UserTime.Month.ToString("00"), post.UserTime.Day.ToString("00"), post.ParentID);
			}
            // by default create link for internal blog post
            else
            {
                if (post.HasName)
                    return FormatUrl("weblogpostName", weblog.ApplicationKey, post.UserTime.Year, post.UserTime.Month.ToString("00"), post.UserTime.Day.ToString("00"), Globals.UrlEncodeFileComponent(post.Name));
                else
                    return FormatUrl("weblogpostId", weblog.ApplicationKey, post.UserTime.Year, post.UserTime.Month.ToString("00"), post.UserTime.Day.ToString("00"), post.PostID);
            }
        }

        public virtual string Post(WeblogPost parent, Weblog weblog, WeblogPost child)
        {
            return string.Format("{0}#{1}",Post(parent,weblog),child.PostID);
        }

        public virtual string IndividualCategoryPage(string applicationKey, int categoryID)
        {
            //if(ct == CategoryType.BlogPost)
                return FormatUrl("weblogpostcategory",applicationKey,categoryID);
            //else
            //    return FormatUrl("weblogarticlecategory",applicationKey,categoryID);
        }

		public virtual string IndividualCategoryPage(Weblog w, string name)
		{
			int id = PostCategories.GetCategory(name,w.SectionID,false).CategoryID;
			return IndividualCategoryPage(w.ApplicationKey,id);
		}

		public virtual string TagsHome(string applicationKey)
		{
			return FormatUrl("weblogpostalltags", applicationKey);
		}

		public virtual string TagsBrowser(string applicationKey, string[] tagNames)
		{
			System.Text.StringBuilder sb = new System.Text.StringBuilder();
			foreach (string tag in tagNames)
			{
				if (sb.Length > 0)
					sb.Append("/");

				sb.Append(Globals.UrlEncodePathComponent(tag));
			}

			return FormatUrl("weblogposttags", applicationKey, sb.ToString());
		}

        public virtual string CommentPosted(WeblogPost post, Weblog weblog)
        {
            string url =Post(post,weblog);
            return string.Format("{0}?CommentPosted=true#commentmessage",url);
        }

		public virtual string CustomCSS(string applicationkey)
		{
			return FormatUrl("blog_customcss",applicationkey);
		}

		public virtual string EmailSubscriptions(string applicationkey)
		{
			return FormatUrl("weblogEmailSubscription",applicationkey);
		}

		public virtual string AttachmentUrl(Weblog blog, PostAttachmentMetaData metaData)
		{
			if(metaData.IsRemote)
				return metaData.FileName;

			WeblogAttachmentSettings fs = WeblogConfiguration.Instance().AttachmentSettings;
			if(fs.EnableDirectLinks)
				return CSContext.Current.Context.Response.ApplyAppPathModifier(fs.DirectPath(metaData));

			return FormatUrl("weblogattachment", blog.ApplicationKey,metaData.PostID);

            
		}

		public virtual string About(string applicationkey)
		{
			return FormatUrl("weblogabout", applicationkey);
		}

		public virtual string Archive(string applicationkey)
		{
			return FormatUrl("weblogarchive", applicationkey);
		}

		public virtual string LinkList(string applicationkey)
		{
			return FormatUrl("webloglinklist", applicationkey);
		}

		public virtual string ContactForm(string applicationkey)
		{
			return FormatUrl("weblogcontact", applicationkey);
		}

		public virtual string IndividualSearch(Weblog blog, string searchTerms, int pageID)
		{
            
			return FormatUrl("weblog_IndividualSearch",blog.ApplicationKey,Globals.UrlEncode(searchTerms),pageID);
		}

		public virtual string IndividualSearch(string applicationKey)
		{
			return FormatUrl("weblog_IndividualSearchPage", applicationKey);
		}

		
		public virtual string TrackbackPingUrl(string applicationKey, int postID)
		{
            return FormatUrl("blog_TrackBack_Ping", applicationKey, postID);
		}
        
		public virtual string PingBackServiceUrl(string applicationKey)
		{
            return FormatUrl("blog_PingBackService", applicationKey);
		}

		#endregion

		#region ControlPanel Urls

		public virtual string ControlPanel_Blogs
		{
			get { return FormatUrl("blog_ControlPanel_Home"); }
		}
		public virtual string ControlPanel_Blogs_Switch
		{
			get { return FormatUrl("blog_ControlPanel_Switch"); }
		}
		
		public virtual string ControlPanel_Blogs_Switch_ReturnUrl(string url)
		{
			return FormatUrl("blog_ControlPanel_Switch_ReturnUrl", url);
		}

		public virtual string PostControlPanel(Weblog w)
		{
			return FormatUrl("blog_ControlPanel_Posts", w.SectionID);
		}


        public virtual string ReferralToPost(string applicationKey, int postID)
        {
            return FormatUrl("blog_referrals_to_post_appkey", applicationKey, postID);
        }
        public virtual string ReferralToPost(int sectionID, int postID)
        {
            return FormatUrl("blog_referrals_to_post", sectionID, postID);
        }

        public virtual string AdminFeedback(string applicationKey)
        {
            return FormatUrl("blog_feedback_appkey", applicationKey);
        }
        public virtual string AdminFeedback(int sectionID)
        {
            return FormatUrl("blog_feedback", sectionID);
        }

        public virtual string AdminFeedbackAction(int sectionID, string action, int postid)
        {
            return FormatUrl("blog_feedback_action", sectionID, action, postid);
        }


        public virtual string AdminFeedback(int sectionID, int postID)
        {
            return FormatUrl("blog_feedback_post", sectionID, postID);
        }
        public virtual string AdminFeedback(int sectionID, BlogPostType postType)
        {
            return FormatUrl("blog_feedback_bpt", sectionID, (int)postType);
        }

        public virtual string AdminFeedbackEditor(string applicationKey)
        {
            return FormatUrl("blogfeedbackeditor", applicationKey);
        }

        public virtual string PostEditor(string applicationKey)
        {
            return FormatUrl("blog_posteditor_appkey", applicationKey);
        }

        public virtual string PostEditor(int sectionID)
        {
            return FormatUrl("blog_posteditor", sectionID);
        }

        public virtual string PostEditor(int sectionID, BlogPostType bpt)
        {
            return FormatUrl("blog_posteditor_bpt", sectionID, (int)bpt);
        }

        public virtual string PostEditor(string applicationKey, BlogPostType bpt)
        {
            return FormatUrl("blog_posteditor_bpt_appkey", applicationKey, (int)bpt);
        }

        public virtual string PostEditor(string applicationKey, int postToEdit)
        {
            return FormatUrl("blog_posteditor_postToEdit_appkey", applicationKey, postToEdit);
        }

        public virtual string PostEditor(int sectionID, int postToEdit)
        {
            return FormatUrl("blog_posteditor_postToEdit", sectionID, postToEdit);
        }


        #endregion

		#region Obsolete

		//        public virtual string AggregateSearch(string searchTerms, int pageID, int groupID)
		//        {
		//            if(groupID <= -1)
		//                    return FormatUrl("weblog_AggregateSearch",Globals.UrlEncode(searchTerms),pageID);
		//                else
		//                    return FormatUrl("weblog_AggregateSearchByGroupID",Globals.UrlEncode(searchTerms),pageID, groupID);
		//        }

		#endregion

        #endregion

		

    }
}
